package com.victualler.product.employee.service.impl;

import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.common.enums.ChecKDataErrorEnum;
import com.victualler.product.employee.dto.EmployeeInfoDto;
import com.victualler.product.employee.entity.EmployeeDao;
import com.victualler.product.employee.entity.EmployeeItemDao;
import com.victualler.product.employee.repository.EmployeeQueryRepository;
import com.victualler.product.employee.repository.EmployeeRepository;
import com.victualler.product.employee.service.EmployeeItemService;
import com.victualler.product.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<EmployeeDao,EmployeeRepository,EmployeeQueryRepository> implements EmployeeService {

    //员工联系人信息
    @Autowired
    private EmployeeItemService employeeItemService;

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 员工全部信息
     * @Function 根据id获取所有信息
     */
    public EmployeeInfoDto findInfoDto(Long id) {
        EmployeeDao employeeDao = this.getById(id);
        List<EmployeeItemDao> employeeItemDaoList = employeeItemService.getByParentId(id);
        return new EmployeeInfoDto(employeeDao,employeeItemDaoList);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param employeeInfoDto 员工全部信息
     * @Function 保存员工大对象
     */
    @Transactional
    public void saveInfo(EmployeeInfoDto employeeInfoDto) {
        //保存
        if(employeeInfoDto.getEmployeeDao().getId() == null) {
            this.save(employeeInfoDto.getEmployeeDao());
            for (EmployeeItemDao employeeItemDao : employeeInfoDto.getItemDetailDaoList()) {
                employeeItemDao.setParentId(employeeInfoDto.getEmployeeDao().getId());
            }
            this.employeeItemService.saveAll(employeeInfoDto.getItemDetailDaoList());
        } else {
            //更新
            this.save(employeeInfoDto.getEmployeeDao());
            this.employeeItemService.updateList(employeeInfoDto.getItemDetailDaoList(),employeeInfoDto.getEmployeeDao().getId());
        }
    }


    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 员工id
     * @param loginAccount 登陆账号
     * @Function 绑定员工账号
     */
    @Transactional
    public void bindAccountNo(Long id,String loginAccount) {
        EmployeeDao emp = this.getByLoginAccount(loginAccount);
        if(emp != null && emp.getId() != id) {
            throw ChecKDataErrorEnum.createException(ChecKDataErrorEnum.CONFIG_ERROR,"登陆账户["+loginAccount + "],已被分配员工[" + emp.getName() + "]");
        } else if (emp != null && emp.getId() == id){
            return;
        }
        EmployeeDao employeeDao = this.getById(id);
        employeeDao.setLoginAccount(loginAccount);
        this.save(employeeDao);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param loginAccount 登陆账号
     * @Function 根据账号获取对应员工
     */
    public EmployeeDao getByLoginAccount(String loginAccount) {
        return this.baseRepository.findByLoginAccount(loginAccount);
    }
}
